package me.zk.practice1.thread;/**
 * @Author: zhang kai
 * @Date: 2021/12/22
 * @Description:
 */

import javafx.application.Application;
import javafx.stage.Stage;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(" t1 await");
                condition.await();
                System.out.println(" t1 run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"t1").start();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                lock.lock();
                System.out.println(" t2 await");
                condition.await();
                System.out.println("t2 run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"t2").start();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                lock.lock();
                System.out.println("t3 signal");
                condition.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"t3").start();
    }
}
